import PlayerBaseData from "../../lib/PlayerBaseData";

export default class PlayerData extends PlayerBaseData{
    private static _ins:PlayerData;
    public currItemType:number=0;
    public unlockItemtypes="0";
    public currGate:number=1;
    public live:number=5;
    public liveTime:number=0;
    public static addGoldVideoMax:number=10;
    public lookaddGoldVideoTimes:number=0;
    public static addItemVideoMax:number=20;
    public lookaddItemVideoTimes:number=0;
    public hisScore:number=0;
    public openId:string="";
    public head:string="";
    public maxCd:number=1800;
    public noticeId:number=0;
    constructor(){
        super();
    }

    public static getIns():PlayerData{
        if(!this._ins)this._ins=new PlayerData();
        return this._ins;
    }
    public initData(data){
        super.initData(data);
        this.checkLiveTime();
       
    }

    public nextGate(){
        this.currGate++;
        this.save();
    }

    public changeItemType(_type:number){
        this.currItemType=_type;
        this.save();
    }

    public checkLiveTime(){
        if(!this.isMaxLive()){
            let time = new Date().getTime();
            let tcd:number=this.maxCd*1000;
            let ttimes=Math.floor((time-this.liveTime)/tcd);
            if(ttimes>0){
                this.addLive(ttimes);
            }
        }
    }

    public addLive(_value:number=1){
        this.live += _value;
        if(this.isMaxLive())this.live=5;
        else if(this.live<0)this.live=0;
        if(_value>0)this.liveTime=new Date().getTime();
        this.save();
    }

    public isMaxLive(){
        return this.live>=5
    }

    public unlockItemType(_id:number){
        if(!this.isUnlockItemType(_id)){
            this.unlockItemtypes += "|"+_id;
            this.save();
            return true;
        }
        return false;
    }

    public isUnlockItemType(_id:number):boolean{
        let tarr = this.unlockItemtypes.split("|");
        return tarr.indexOf(_id+"")>=0;
    }

    public addGoldVideoTimes(){
        this.lookaddGoldVideoTimes++;
        this.save();
    }

    public isMaxAddGoldVideoTimes(){
        return this.lookaddGoldVideoTimes>=PlayerData.addItemVideoMax;
    }

    public addItemVideotimes(){
        this.lookaddItemVideoTimes++;
        this.save();
    }

    public isMaxAddItemVideoTimes(){
        return this.lookaddItemVideoTimes>=PlayerData.addItemVideoMax;
    }


    public dayRest(){
        super.dayRest();
        this.lookaddGoldVideoTimes=0;
        this.lookaddItemVideoTimes=0;
    }

    public setHisScore(_value:number){
        if(_value>this.hisScore){
            this.hisScore=_value;
            this.save();
            return true;
        }
        return false;
    }
    
    public clearHisScore(){
        this.hisScore=0;
        this.save();
    }

    public setName(_name:string){
        if(this.name != _name){
            this.name=_name;
            this.save();
            return true;
        }
        return false;
    }


    public createId(){
        let tid="y"+Math.floor(Math.random()*1000)+"_"+new Date().getTime();
        this.id=tid;
        if(!this.name)this.name="佚名";
        this.save();
        return tid;
    }
}